package cn.net.wanji.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * @author ：mmzsit
 * @description：
 * @date ：2019/6/13 10:32
 */
public class DateUtils {
    private static SimpleDateFormat sdf;
    private static DateTimeFormatter formatter12 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static DateTimeFormatter formatter13 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
    /**
     * 获取当前时间yyyyMMddHHmm
     */
    public static String getCurrentDate(String pattern) {
        sdf = new SimpleDateFormat(pattern);
        String curDate = sdf.format(new Date());
        return curDate;
    }

    /**
     * 获取昨天的时间yyyyMMdd
     */
    public static String getLastDate() {
        sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.DATE, -1); //年份减1
        Date lastDay = ca.getTime(); //结果
        return sdf.format(lastDay);
    }

    /**
     * 获取当前时间
     */
    public static Date getCurrentDateAndTime() {
        sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        String curDate = sdf.format(new Date());
        try {
            return sdf.parse(curDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取当前时间并转化为类似20190601的格式
     * @param dataStr
     * @return
     * @throws ParseException
     */
    public static String get_MM01Str(String dataStr) throws ParseException {
        return getStrByDateFormat("yyyyMM01", getDatebyFormaterStr("yyyyMMdd", dataStr));
    }
    public static Date getDatebyFormaterStr(String formater, String dateStr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(formater);
        return sdf.parse(dateStr);
    }

    public static String getStrByDateFormat(String formater, Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(formater);
        return sdf.format(date);
    }

    public static String getStrFormatYyyyMmdd(String datetimeStr) {
        if (datetimeStr.contains("T")) {
            datetimeStr = StringUtils.timeRevise(datetimeStr);
        }
        // 定义日期时间格式
        DateTimeFormatter formatter;
        if (StringUtils.isNotNull(datetimeStr) && datetimeStr.contains(".")) {
             formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        }else {
             formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        }


        // 解析字符串为LocalDateTime对象
        LocalDate date = LocalDate.parse(datetimeStr, formatter);

        // 提取日期部分（年月日）
        String dateStr = date.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        return dateStr;
    }


//    public static String getStrFormatYyyyMmddHHMmSs(String datetimeStr) {
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
//
//        LocalDateTime dateTime = LocalDateTime.parse(datetimeStr, formatter);
//        String dateStr = dateTime.toString();
//
//        return StringUtils.timeRevise(dateStr);
//    }

    public static String getStrFormatYyyy_Mm_dd(String datetimeStr) {
        // 定义日期时间格式
        DateTimeFormatter formatter;
        if (StringUtils.isNotNull(datetimeStr) && datetimeStr.contains(".")) {
            formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        }else {
            formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        }


        // 解析字符串为LocalDateTime对象
        LocalDate date = LocalDate.parse(datetimeStr, formatter);

        // 提取日期部分（年月日）
        String dateStr = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        return dateStr;
    }
    public static String getStrFormatYyyyMmddHHMmSs(String datetimeStr) {
        // Define date-time format
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");

        // Parse string to LocalDateTime object
        LocalDateTime dateTime;
        try {
            dateTime = LocalDateTime.parse(datetimeStr, formatter);
        } catch (Exception e) {
            // Parsing failed, try another format
            formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            dateTime = LocalDateTime.parse(datetimeStr, formatter);
        }

        // Format date-time object to desired format
        return dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static LocalDateTime getDetectTimeAsLocalDateTime(String detectTime) {
        if (!StringUtils.isEmpty(detectTime) && detectTime.contains(".")) {
            return LocalDateTime.parse(detectTime,formatter13);
        }else if(!StringUtils.isEmpty(detectTime)){
            return LocalDateTime.parse(detectTime,formatter12);
        }else {
            return LocalDateTime.parse(LocalDateTime.now().toString(),formatter12);
        }
    }

    /**
     * 测试
     * @param args
     * @throws ParseException
     */
    public static void main(String[] args) throws ParseException {
        LocalDateTime detectTimeAsLocalDateTime = getDetectTimeAsLocalDateTime("2024-06-17 15:26:04");
        System.out.println(detectTimeAsLocalDateTime);

//        String currentTime = DateUtil.getCurrentDate("yyyy-MM-dd HH:mm");
//        System.out.println("current:" + currentTime);
//        String currentDate = DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss");
//        System.out.println("currenDate:" + currentDate);
        String strFormatYyyy_mm_dd = getStrFormatYyyyMmdd("2024-06-17 15:26:04.5");
        System.out.println(strFormatYyyy_mm_dd);
    }
}
